<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="菜品名称" prop="dishesName">
        <el-input
          v-model="queryParams.dishesName"
          placeholder="请输入菜品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统一管理" prop="packageId">
        <el-select
          v-model="queryParams.packageId"
          placeholder="是否统一管理"
          clearable
          @keyup.enter.native="handleQuery">
          <el-option
            v-for="status in dict.type.sys_dishes_shopping"
            :key="status.value"
            :label="status.label"
            :value="status.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="菜品状态" prop="dishesStatus">
        <el-select
          v-model="queryParams.dishesStatus"
          placeholder="是否在售"
          clearable
          @keyup.enter.native="handleQuery">
          <el-option
          v-for="status in dict.type.sys_dishes_status"
          :key="status.value"
          :label="status.label"
          :value="status.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dishes:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:dishes:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dishes:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:dishes:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dishesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="菜品名称" align="center" prop="dishesName" />
      <el-table-column label="菜品图片" align="center" prop="dishesImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.dishesImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="菜品分类" align="center" prop="mainType" >
        <template slot-scope="scope">
          <span v-if="scope.row.mainType==='1'">凉菜</span>
          <span v-else-if="scope.row.mainType==='0'">热菜</span>
          <span v-else-if="scope.row.mainType==='2'">主食</span>
          <span v-else-if="scope.row.mainType==='3'">饮品</span>
        </template>
      </el-table-column>
      <el-table-column label="菜品描述" align="center" prop="dishesDescription" />
      <el-table-column label="统一管理" align="center" prop="packageId" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.packageId"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="菜品价格" align="center" prop="dishesPrice" />
      <el-table-column label="菜品状态" align="center" prop="dishesStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.dishesStatus === '1'" type="warning" size="small">下架</el-tag>
          <el-tag v-else type="success" size="small">在售</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dishes:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dishes:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改菜品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="菜品分类" prop="mainType">
          <el-select v-model="form.mainType" placeholder="请选择主要菜品类型">
            <el-option value="3" label="饮品">饮品</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜品名称" prop="dishesName">
          <el-input v-model="form.dishesName" placeholder="请输入菜品名称" />
        </el-form-item>
        <el-form-item label="菜品价格" prop="dishesPrice">
          <el-input v-model="form.dishesPrice" placeholder="请输入菜品价格" />
        </el-form-item>
        <el-form-item label="菜品图片" prop="dishesImage">
          <image-upload v-model="form.dishesImage">
            <el-image src="form.dishesImage"/>
          </image-upload>
        </el-form-item>
        <el-form-item label="菜品描述" prop="dishesDescription">
          <el-input v-model="form.dishesDescription" placeholder="请输入菜品描述" />
        </el-form-item>
        <el-form-item label="菜品状态" prop="dishesStatus">
          <el-select  v-model="form.dishesStatus" placeholder="请选择菜品状态">
            <el-option
            v-for="status in dict.type.sys_dishes_status"
            :key="status.value"
            :label="status.label"
            :value="status.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDishes,
  getDishes,
  delDishes,
  addDishes,
  updateDishes,
  changeDishesStatus,
  shoppingChange
} from "@/api/system/drinkDishes";

export default {
  dicts:['sys_dishes_type','sys_dishes_status','sys_dishes_shopping'],
  name: "Dishes",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 菜品管理表格数据
      dishesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dishesName: null,
        packageId: null,
        mainType: null,
        otherType: null,
        shoppingChange: null,
        dishesImage: null,
        dishesStatus: null,
        dishesDescription: null,
        dishesPrice: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dishesName: [
          { required: true, message: "菜品名称不能为空", trigger: "blur" }
        ],
        mainType: [
          { required: true, message: "菜品主要分类 0热菜1凉菜不能为空", trigger: "change" }
        ],
        shoppingChange: [
          { required: true, message: "是否进入购物车0进入1未进入不能为空", trigger: "blur" }
        ],
        dishesStatus: [
          { required: true, message: "菜品状态0上架1下架不能为空", trigger: "change" }
        ],
        dishesDescription: [
          { required: true, message: "菜品状态0上架1下架不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询菜品管理列表 */
    getList() {
      this.loading = true;
      listDishes(this.queryParams).then(response => {
        this.dishesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dishesId: null,
        dishesName: null,
        packageId: null,
        mainType: null,
        otherType: null,
        shoppingChange: null,
        dishesImage: null,
        dishesStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dishesId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加菜品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dishesId = row.dishesId || this.ids
      getDishes(dishesId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改菜品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dishesId != null) {
            updateDishes(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDishes(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dishesIds = row.dishesId || this.ids;
      this.$modal.confirm('是否确认删除菜品管理编号为"' + dishesIds + '"的数据项？').then(function () {
        return delDishes(dishesIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dishes/export', {
        ...this.queryParams
      }, `dishes_${new Date().getTime()}.xlsx`)
    },
    handleStatusChange(row) {
      let text = row.packageId === "0" ? "移入" : "移出";
      this.$modal.confirm('确认要"' + text + '""' + row.dishesName + '"菜品吗？').then(function () {
        return changeDishesStatus(row.dishesId, row.packageId);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.packageId = row.packageId === "0" ? "1" : "0";
      });
    },
    handleChange(row) {
      shoppingChange(row.dishesId, row.shoppingChange);
      this.$message({
        message: '操作成功，请在购物车中查看',
        type: 'success'
      });

    },
  }
};
</script>
